package com.ck.ips.controller.sys;

import com.ck.common.model.Role;
import com.ck.common.page.PageInfo;
import com.ck.core.annotation.LoginRequired;
import com.ck.core.annotation.SysLog;
import com.ck.core.constant.ModuleConstant;
import com.ck.ips.controller.BaseController;
import com.ck.ips.model.vo.sys.RoleListVO;
import com.ck.ips.service.sys.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;

import static com.ck.core.util.StringUtils.listToFormatString;

/**
* @Description: 角色controller
* @Author: hf
* @Date:    2019/4/30 16:31
* @Version: 1.0
*/
@LoginRequired
@RestController
@RequestMapping("/role")
public class RoleController extends BaseController {
	@Autowired
	private RoleService roleService;

	/**
	 * 保存角色
	 */
	@SysLog(module = ModuleConstant.MODULE_ROLE)
	@PostMapping("/save")
	public Object saveRole(Integer status, String roleDesc, String roleName, String moduleIds,String deviceGroupIds) {
		Role role = new Role();
		role.setStatus(status);
		role.setRoleDesc(roleDesc);
		role.setRoleName(roleName);
		return roleService.insertRole(role, moduleIds,deviceGroupIds);
	}

	/**
	 * 修改角色
	 */
	@SysLog(module = ModuleConstant.MODULE_ROLE)
	@PostMapping("/update")
	public Object updateRole(String moduleIds, String deviceGroupIds, Long roleId, Integer status, String roleDesc, String roleName) {
		Role role = new Role();
		role.setRoleId(roleId);
		role.setStatus(status);
		role.setRoleDesc(roleDesc);
		role.setRoleName(roleName);
		return roleService.updateRole(role,moduleIds,deviceGroupIds);
	}

	/**
	 * 删除角色
	 */
	@SysLog(module = ModuleConstant.MODULE_ROLE)
	@PostMapping("/delete")
	public Object deleteRole( Long roleId ) {
		return roleService.deleteRole(roleId);
	}

	/**
	 * 角色列表
	 */
	@PostMapping("/list")
	public Object listRole(Integer status, String roleName, Integer pageNum, Integer pageSize) {
		PageInfo<RoleListVO> pageInfo = roleService.listRole( status, roleName, pageNum, pageSize);
		return pageInfo;
	}

	/**
	 * 获取角色选择列表
	 */
	@PostMapping("/listRoleSelect")
	public Object listRoleSelect() {
		return roleService.listRoleSelect();
	}

	/**
	 * 启用禁用角色
	 */
	@SysLog(module = ModuleConstant.MODULE_ROLE)
	@PostMapping(value = "/enableDisableRole")
	public Object enableDisableRole(Long roleId,Integer status) {
		return roleService.enableDisableRole(roleId,status);
	}

	/**
	 * 获取角色拥有的权限和id
	 */
	@PostMapping(value = "/listModuleNamesByRoleId")
	public Object listModuleNamesByRoleId(Long roleId) {
		HashMap<String,String> resultMap = new HashMap<>(2);
		String authName = listToFormatString(roleService.listModuleNamesByRoleId(roleId), ",");
		resultMap.put("authName",authName);
		List<String> moduleIdList = roleService.listModuleIdsByRoleId(roleId);
		String authId = listToFormatString(moduleIdList,",");
		resultMap.put("authId",authId);
		return resultMap;
	}

}
